
/*****Drop Variables****

Pre-cond. 
1. No Consent - "drop_consent"
2. No Audio Capability - "drop_audio"
3. Not US Citizen - "drop_citizen"
4. Weather Drop - Attention Check Failed: "drop_weather"
5. Inconsistent Party - Different party from Recruitment: 
	"drop_inconsistent"

1. Took survey in U.S. - "drop_us"
2. Content Check of Treatment: "drop_contentpost"
3. Time of Survey: "drop_time"
4. Missing Pre-treatment Covariates: "drop_missing"

***********************/

clear all

**** Install packages ***
*ssc install pdslasso
*ssc install lassopack

********************
*Set Person Working*
*Change this to your name!!!!!
********************
*global carolina 0
*global emily 1

global emily 1
global carolina 0

*********************
*Load Files*
*********************
if $emily ==1  {
*Emily's Computer*
cap cd "~/Dropbox/SourceContent/Data/Study"
global out "../../Results/"
} 

*Carolina's Computer*
if $carolina ==1  {
cap cd "E:\Dropbox\Carolina-Emily-Project\Data\Study"
global out "..\..\Results\"

}

use 2B_clean_data, replace

****************************
****Counts of Restrictions**
****************************

*Initial Drops - Kick out of Survey
*N=21,717
count if drop_consent | drop_audio | drop_citizen | drop_inconsistent
*drop=4,255
drop if drop_consent | drop_audio | drop_citizen | drop_inconsistent
*N=18,199

*Weather attention - Kick out of survey
count if drop_weather==1
*drop=3,843
drop if drop_weather==1
*N=14,356

*Sample in the U.S.
count if drop_us==1
*drop=360
*N=13,996
count if drop_us==1 & drop_missing==0 & drop_content==0 & drop_time==0

*Missing Treatment Responses
count if drop_missing
*drop=466
count if drop_us==0 & drop_missing==1 & drop_content==0 & drop_time==0

*Content Post Check 
count if drop_content==1 
*drop=467
count if drop_content==1 & drop_time==0 & drop_missing==0 & drop_us==0
*297 of the 467 people!!

*Content Post Check - + Winsorized*
tab treat drop_content if prob_index_anti==1
count if drop_content==1 & drop_time==0 & drop_missing==0 & drop_us==0 & prob_index_anti!=1
count if drop_content==1 & drop_time==0 & drop_missing==0 & drop_us==0 & prob_index_anti==1
*Only 12 people!*

*Time Restriction
count if drop_time==1
*drop=441
count if drop_us==0 & drop_missing==0 & drop_content==0 & drop_time==1

/*Count if only dropped because of failcontent
drop sample
gen sample=drop_us | drop_missing | failcontent_discrete | drop_time
*/
**************************
**************************

*drop_us drop_inconsistent drop_weather drop_contentpost drop_time drop_missing

*Robustness Versions*

*1. Base
global spec1covar0 	"$lasso0"
global spec1covar1 	"$lasso1"
global spec1restrict	"& sample==1"

*2. Keep People who take outside the U.S.
global spec2covar0 		"$lasso0 drop_missing"
global spec2covar1 		"$lasso1 drop_missing"
global spec2restrict	"& (drop_us==1 | sample==1)" 

*3. Keep People who Don't meet Time Restriction
global spec3covar0 		"$lasso0 drop_missing"
global spec3covar1 		"$lasso1 drop_missing"
global spec3restrict	"& (drop_time==1 | sample==1)" 

*4. Keep People who don't have complete pre-treatment responses
global spec4covar0 		"$lasso0 drop_missing"
global spec4covar1 		"$lasso1 drop_missing"
global spec4restrict	"& (drop_missing==1 | sample==1)" 

*5. Keep People who fail the content post questions
global spec5covar0 		"$lasso0 drop_missing"
global spec5covar1 		"$lasso1 drop_missing"
global spec5restrict	"& (drop_contentpost==1 | sample==1)" 

*6. Keep People who fail the content post questions (unique)
global spec6covar0 		"$lasso0 drop_missing"
global spec6covar1 		"$lasso1 drop_missing"
global spec6restrict	"& (drop_contentpost==1 | sample==1) & drop_time==0 & drop_missing==0 & drop_us==0" 

*7. Keep People who fail the content post questions BUT drop prob_anti==1 (outliers)
global spec7covar0 		"$lasso0 drop_missing"
global spec7covar1 		"$lasso1 drop_missing"
global spec7restrict	"& (drop_contentpost==1 | sample==1) & (prob_index_anti!=1)" 

gen occ_nyt=freq_nytimes==1
gen week_nyt=freq_nytimes==2
gen daily_nyt=freq_nytimes==3
gen occ_fox=freq_fox==1
*gen week_fox=freq_fox==2
gen daily_fox=freq_fox==3

global lasso0 hispanic age55_64 candidate16_hillary candidate16_other ///
	immlevel_decrease gun_lessstrict abortion_illegal health_notgvt ///
	occ_twitter occ_buzzfeed trumpfan lebronfan taylorfan bgatesfan ///
	obamafan obama_neutral topissue_health 
	
global lasso1 black hispanic age35_44 age45_54 hsdegree ///
	candidate16_hillary immlevel_decrease immlevel_same ///
	gun_same gun_lessstrict abortion_partlegal abortion_illegal tax_toohigh ///
	health_neutral health_notgvt occ_nyt week_nyt daily_nyt ///
	daily_tv occ_tv daily_newspaper occ_fox week_fox daily_fox week_breitbart ///
	daily_breitbart occ_buzzfeed ///
	lebron_neutral obamafan trump_neutral trumpfan west topissue_tax 	
	
foreach x in $lasso0 $lasso1 {
		replace `x'=0 if drop_missing==1
		}

*********************************
***Create Variables and Format***
*********************************

global message1 "Anti"
global message2 "Anti"
global message3 "Pro"
global message4 "Pro"

global source1 "Trump"
global source2 "Obama"
global source3 "Trump"
global source4 "Obama" 

global party0 "Republicans"
global party1 "Democrats"
global var0 "ln(Probability Anti-Immigrant + 1)"
global var1 "ln(Probability Pro-Immigrant + 1)"
global dir0 "anti"
global dir1 "pro"

global color1 "blue"
global color0 "red"

global label1a0 "{bf:Anti}	"
global label2a0 "{bf:Anti}	"
global label3a0 "{bf:Pro}	"
global label4a0 "{bf:Pro}	"
global label1b0 "{bf:Trump}	"
global label2b0 "{bf:Obama}	"
global label3b0 "{bf:Trump}	"
global label4b0 "{bf:Obama}	"

foreach x in a b {
foreach y in 1 2 3 4 {
	global label`y'`x'1 ""
}
}

**************************************************
*CREATE Variables*
**************************************************

preserve

gen group1=treatment==5 | treatment==7 | treatment==0
gen group2=treatment==6 | treatment==8 | treatment==0
gen group3=treatment==1 | treatment==3 | treatment==0
gen group4=treatment==2 | treatment==4 | treatment==0

gen president=treatment==1 | treatment==2 | treatment==5 | treatment==6
gen actor=treatment==3 | treatment==4 | treatment==7 | treatment==8
gen turkey= treatment==9 | treatment==10
gen priming=turkey==1 | president==1
gen message=actor==1 | president==1

**************************************************
*1. Moderate/Extreme*
**************************************************


foreach m in "m" "ms" {
foreach p in 0 1 {
	gen b`m'`p'=.
	gen u`m'`p'=.
	gen l`m'`p'=.
}
}


gen n=_n

foreach p in 0 1 {

local n=0
	
foreach y in 4 3 2 1 {

foreach s in 7 6 5 4 3 2 1 {
	
	local n=`n'+1
 
	*Regression for Coefficients*
	reg ln1_prob_index_${dir`p'} message president ${spec`s'covar`p'} ///
		if (group`y'==1) & recruit==`p' ${spec`s'restrict}, robust
	
	*Anonymous Message - Beta_m*
	replace bm`p' = _b[message] if n==`n'
	replace um`p' = _b[message]+1.96*_se[message] if n==`n'
	replace lm`p' = _b[message]-1.96*_se[message] if n==`n'

	*Persuasion - Beta_ms *	
	replace bms`p' = _b[president] if n==`n'
	replace ums`p' = _b[president]+1.96*_se[president] if n==`n'
	replace lms`p' = _b[president]-1.96*_se[president] if n==`n'
	
	*Anonymous Message - P(Dist)*
	reg ln1_prob_index_${dir`p'} ${spec`s'covar`p'} ///
		if (group`y'==1 & president==0) & recruit==`p' ${spec`s'restrict}, robust
	predict res, res
	gen g=0 if actor==0 & e(sample)==1
	replace g=1 if actor==1 & e(sample)==1
	ksmirnov res if e(sample), by(g)
	local dm`n'`p'=round(r(p)*10000)/10000
	local dm`n'`p': di %6.3f `dm`n'`p''
	drop g res
	
	*Persuasion - P(Dist)*
	reg ln1_prob_index_${dir`p'} ${spec`s'covar`p'} ///
		if (group`y'==1 & treat!=0) & recruit==`p' ${spec`s'restrict}, robust
	predict res, res
	gen g=0 if president==0 & e(sample)==1
	replace g=1 if president==1 & e(sample)==1
	ksmirnov res if e(sample), by(g)
	local dms`n'`p'=round(r(p)*10000)/10000
	local dms`n'`p': di %6.3f `dms`n'`p''
	drop g res
	
	}
	
	local n=`n'+1
	
}

foreach m in "m" "ms" {

# delimit ;
twoway	
	(rcap l`m'`p' u`m'`p' n if 	n==7 | n==15 | n==23 | n==31 , horizontal lcolor(black)) 
	(scatter n b`m'`p'  if 		n==7 | n==15 | n==23 | n==31 , mcolor(black) msymbol(circle) )	
	(rcap l`m'`p' u`m'`p' n if 	n==6 | n==14 | n==22 | n==30 , horizontal lcolor(black)) 
	(scatter n b`m'`p'  if 		n==6 | n==14 | n==22 | n==30 , mcolor(black) msymbol(circle_hollow) )	
	(rcap l`m'`p' u`m'`p' n if 	n==5 | n==13 | n==21 | n==29 , horizontal lcolor(black)) 
	(scatter n b`m'`p' if 		n==5 | n==13 | n==21 | n==29 , mcolor(black) msymbol(square_hollow) )	
	(rcap l`m'`p' u`m'`p' n if 	n==4 | n==12 | n==20 | n==28 , horizontal lcolor(black)) 
	(scatter n b`m'`p' if 		n==4 | n==12 | n==20 | n==28 , mcolor(black) msymbol(diamond_hollow) )	
	(rcap l`m'`p' u`m'`p' n if 	n==3 | n==11 | n==19 | n==27 , horizontal lcolor(black)) 
	(scatter n b`m'`p' if 		n==3 | n==11 | n==19 | n==27 , mcolor(black) msymbol(triangle_hollow) )	
	(rcap l`m'`p' u`m'`p' n if 	n==2 | n==10 | n==18 | n==26 , horizontal lcolor(black)) 
	(scatter n b`m'`p' if 		n==2 | n==10 | n==18 | n==26 , mcolor(black) msymbol(X) )	
	(rcap l`m'`p' u`m'`p' n if 	n==1 | n==9  | n==17 | n==25 , horizontal lcolor(black)) 
	(scatter n b`m'`p' if 		n==1 | n==9  | n==17 | n==25 , mcolor(black) msymbol(plus) )	
	, 
	xline(0, lcolor(black)) 
	ylabel(
	32	`" "{bf:P(Dist):}" "'
	31	`" "${label1a`p'}{it:`d`m'31`p''}" "'
	30	`" "${label1b`p'}{it:`d`m'30`p''}" "'
	29	`" "{it:`d`m'29`p''}" "'
	28	`" "{it:`d`m'28`p''}" "'
	27	`" "{it:`d`m'27`p''}" "'
	26	`" "{it:`d`m'26`p''}" "'
	25	`" "{it:`d`m'25`p''}" "'
	24	" "
	23	`" "${label2a`p'}{it:`d`m'23`p''}" "'
	22	`" "${label2b`p'}{it:`d`m'22`p''}" "'
	21	`" "{it:`d`m'21`p''}" "'
	20	`" "{it:`d`m'20`p''}" "'
	19	`" "{it:`d`m'19`p''}" "'
	18	`" "{it:`d`m'18`p''}" "'
	17	`" "{it:`d`m'17`p''}" "'
	16	" "
	15	`" "${label3a`p'}{it:`d`m'15`p''}" "'
	14	`" "${label3b`p'}{it:`d`m'14`p''}" "'
	13	`" "{it:`d`m'13`p''}" "'
	12	`" "{it:`d`m'12`p''}" "'
	11	`" "{it:`d`m'11`p''}" "'
	10	`" "{it:`d`m'10`p''}" "'
	9	`" "{it:`d`m'9`p''}" "'
	8	" "
	7	`" "${label4a`p'}{it:`d`m'7`p''}" "'
	6	`" "${label4b`p'}{it:`d`m'6`p''}" "'
	5	`" "{it:`d`m'5`p''}" "'
	4	`" "{it:`d`m'4`p''}" "'
	3	`" "{it:`d`m'3`p''}" "'
	2	`" "{it:`d`m'2`p''}" "'
	1	`" "{it:`d`m'1`p''}" "'
	, angle(0) labsize(small)) 
	xtitle(" " "{bf:${party`p'}}" "{it:${var`p'}}" " ")
	ytitle("")
	ylab(, nogrid)
	xlab(-.04(0.02).04) xscale(range( -0.05 0.05) )
	graphregion(color(white)) 
	legend(order(2 "Base"  4 "+ Outside U.S." 6 "+ Long/Short Time" 
	8 "+ Missing Responses" 
	10 "+ Fail Content Check" 
	12 "+ Fail Content Check (Unique)"
	14 "+ Fail Content Check, - Outliers") 
	rows(3) size(*0.75) symxsize(*0.4))
	ylabel(, tlength(0))
	saving("$out/`m'`p'.gph", replace)
	;
	# delimit cr

	}	
}
	
*	imargin(-3 -3 -5 -5) scale(1.1) 
# delimit ;
grc1leg "$out/m0.gph" "$out/m1.gph", 
	ycommon xcommon legendfrom("$out/m0.gph")
	graphregion(color(white)) 
	imargin(-5.5 -5.5 -6 -6) scale(1) 
	;
# delimit cr
graph export "$out/B3_SampleRestrict_AnonymousMessage.pdf", replace	


# delimit ;
grc1leg "$out/ms0.gph" "$out/ms1.gph", 
	ycommon xcommon legendfrom("$out/ms1.gph")
	graphregion(color(white)) 
	imargin(-5.5 -5.5 -6 -6) scale(1) 
	;
# delimit cr
graph export "$out/B3_SampleRestrict_Persuasion.pdf", replace	

restore

keep if sample==1

save 3_clean_data, replace

//Create Outlier Histograms

use 2B_clean_data, replace

drop if drop_consent | drop_audio | drop_citizen | drop_inconsistent ///
	| drop_time | drop_missing | drop_us | drop_weather


# delimit ;
twoway 
(hist prob_index_anti if recruit==0 & sample==1, lcolor(black) fcolor(white) ) 
(hist prob_index_anti if recruit==0 & sample==0, lcolor(black) fcolor(gray) ) 
,
xtitle(" " "Probability Anti-Immigrant" " ")
ytitle(" ")
graphregion(color(white)) ylabel(, nogrid)
legend(order(1 "Republican - Analysis Sample (N=5,974)" 2 "Republican - Fail Content Check (N=135)"
) rows(2))
text(15 0.82 "{it:P(Anti)=1}" "{it:for 129 of 135}" "{it:Responses}")
;
# delimit cr
graph export "$out/B2_Histogram_NoContentCheck_Republicans.pdf", replace	


# delimit ;
twoway 
(hist prob_index_anti if recruit==1 & sample==1, lcolor(black) fcolor(white) )
(hist prob_index_anti if recruit==1 & sample==0, lcolor(black) fcolor(gray)) 
,
xtitle(" " "Probability Anti-Immigrant" " ")
ytitle(" ")
graphregion(color(white)) ylabel(, nogrid)
legend(order(1 "Democrat - Analysis Sample (N=7,125)" 2 "Democrat - Fail Content Check (N=162)"
) rows(2))
text(10 0.8 "{it:P(Anti)=1}" "{it:for 156 of 162}" "{it:Responses}")

;
# delimit cr
graph export "$out/B2_Histogram_NoContentCheck_Democrats.pdf", replace	


